<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/7/5  13:46
 */
namespace app\admin\controller;

class Article extends Common
{
    private $table_admin;

    function __construct()
    {
        parent::__construct();
        $this->table_admin = db('article');
    }

    /*
    服务器、域名列表
    */
    public function index()
    {
        $pageParam = ['query' =>[]];
        $where = [];
        $keyword = input('keyword');
        if( $keyword != ''){
            $where['name'] = array('like','%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
            $this->assign('keyword', $keyword);
        }

        $cid = input('cid');
        $where['cid'] = $cid;

        $article_result = $this->table_admin
            ->where($where)
            ->order('create_time desc')
            ->paginate(15, false, $pageParam)->each(function($item,$key){
                if($item['finish_time'] > time()){
                    $item['time'] = '未到期';
                }else{
                    $item['time'] = '到期';
                }
                return $item;
            });
//        halt($article_result);
        $page  = $article_result->render();
        $this->assign('page', $page);
        $this->assign('article_result', $article_result);

        return $this->fetch('Article/list');
    }

    //添加服务器、域名
    public function add()
    {
        if(request()->isPost()){
            $id = input('id');

            $name = input('name');
            if(!$name){
                $this->error('服务器或域名不为空');
            }
            $cid = input('cid');
            $company = input('company');
            $finish_time = input('finish_time');
            $mobile = input('mobile');

            $data['name'] = $name;
            $data['cid'] = $cid;
            $data['company'] = $company;
            $data['finish_time'] = strtotime($finish_time);
            $data['mobile'] = $mobile;

//halt($data);
            if($id){
                //修改
                $bool = $this->table_admin->where('id='.$id)->update($data);
            }else{
                $data['create_time'] = time();
                //添加
                $bool = $this->table_admin->insert($data);
//                $sql = $this->table_admin->getLastSql();
//                halt($sql);exit;
            }
            if($bool){
                $this->success('操作成功', url('Article/index',array('cid'=>$cid)));
            }else{
                $this->error('操作失败');
            }
        }
        return $this->fetch();
    }

    //修改服务器、域名
    public function edit()
    {
        $id = input('id');
        $data = $this->table_admin->where('id='.$id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    //删除服务器、域名
    public function delete()
    {
        $id = input("id");
        if(!$id) $this->error('参数有误');
        $da=$this->table_admin->where("id='$id'")->find();
        if(!$da) $this->error('数据不存在');
        $res=$this->table_admin->where("id='$id'")->delete();
        if(!$res) $this->error('删除失败');
        $this->success("删除成功!",url('Article/index'));

    }

//    到期提醒
    public function tips()
    {
        $time = strtotime(date('Y-m-d',strtotime('+5 day')));
        $where['finish_time'] = [['>', time()],['<', $time],'and'];
        $article_result = $this->table_admin
            ->where($where)
            ->order('create_time desc')
            ->paginate(15)->each(function($item,$key){
                $day = floor(($item['finish_time']-time())/86400);
                if($day == '0') {
                    $item['time'] = '不到1天';
                }elseif($day == '1') {
                    $item['time'] = '不到2天';
                }elseif($day == '2') {
                    $item['time'] = '不到3天';
                }elseif($day == '3') {
                    $item['time'] = '不到4天';
                }elseif($day == '4') {
                    $item['time'] = '不到5天';
                }
                return $item;
            });
        $this->assign('cname',array('1'=>'服务器','2'=>'域名'));
//        strtotime(date('Y-m-d',strtotime('+1 day')));
//        echo strtotime(date('Y-m-d'))+3600*24;
        $this->assign('count',$article_result->total());
        $page  = $article_result->render();
        $this->assign('page', $page);
        $this->assign('article_result', $article_result);
        $this->assign('time',time('Y-m-d'));//1532938244
        $this->assign('time1',date('Y-m-d'));//1532938244
        $this->assign('time2',time(date('Y-m-d')));//1532938244
        return $this->fetch();
    }

    public function tip()
    {
        $time = time();
        $where['finish_time'] = ['<', $time];
        $article_result = $this->table_admin
            ->where($where)
            ->order('create_time desc')
            ->paginate(15)->each(function($item,$key){
                    $item['time'] = '已过期';
                return $item;
            });
        $this->assign('cname',array('1'=>'服务器','2'=>'域名'));
//        strtotime(date('Y-m-d',strtotime('+1 day')));
//        echo strtotime(date('Y-m-d'))+3600*24;
        $this->assign('count',$article_result->total());
        $page  = $article_result->render();
        $this->assign('page', $page);
        $this->assign('article_result', $article_result);
        $this->assign('time',time('Y-m-d'));//1532938244
        $this->assign('time1',date('Y-m-d'));//1532938244
        $this->assign('time2',time(date('Y-m-d')));//1532938244
        return $this->fetch('Article/tips');
    }
}